#parse('include.java')
#if(!$sp)
#set($sp = $CSU.getSuperPackage($model.package,1))
#end
#set($TYPE = $model.name)
#set($type = $CSU.getVariable($TYPE))
#set($daoName = $type+'DAO')
package ${sp}.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import wangjg.commons.page.Page;

import $!{model.package}.${model.name};
#importOtherEntity($model)
import ${sp}.dao.${model.name}DAO;

@Service
@Transactional
public class ${model.name}ServiceImpl implements ${model.name}Service{

	@Autowired
	private ${TYPE}DAO ${daoName};
#set($refModelList = $MU.getRefModelList($model))
#foreach($m in $refModelList)
#set($mName = $CSU.getSimpleType($m.name))
#if($mName != $TYPE)
	#set($vMName = $CSU.getVariable($mName))
	@Autowired
	private ${mName}DAO ${vMName}DAO;
#end
#end

	@Override
	public ${TYPE} get${TYPE}(Long id ) {
		return this.${daoName}.findById(id);
	}

	@Override
	public void save(${TYPE} ${type}){
		Date now = new Date();
		if(${type}.getId()==null){
			${type}.setCreated(now);
			${type}.setModified(now);
			this.${daoName}.save(${type});
		}else{
			${type}.setModified(now);
			this.${daoName}.update(${type});
		}
	}


	@Override
	public void batchSave(List<${TYPE}> list){
		Date now = new Date();
		for(${TYPE} ${type}:list){
			if(${type}.getId()==null){
				${type}.setCreated(now);
				${type}.setModified(now);
				this.${daoName}.save(${type});
			}else{
			${type}.setModified(now);
			this.${daoName}.update(${type});
			}
		}
	}

	@Override // !
	public void batchSave(List<${TYPE}> list){
		Timestamp now = new Timestamp(System.currentTimeMillis());
		for(${TYPE} ${type}:list){
			if(${type}.getId()==null){
				${type}.setAddTime(now);
				${type}.setUpdateTime(now);
				this.${type}DAO.save(${type});
			}else{
				${type}.setUpdateTime(now);
				this.${type}DAO.update(${type});
			}
		}
	}

	@Override
	public void update(${TYPE} ${type}){
		Date now = new Date();
		${type}.setModified(now);
		this.${daoName}.update(${type});
	}

	@Override
	public void delete(${TYPE} ${type}){
		this.${daoName}.delete(${type});
#trim('eachrow', 2, 'rn')
	#delRelation
#end

	}

	@Override
	public void batchDelete(String idseq) {
		String[] sa = idseq.split(",");
		for(String s:sa){
			Long id = Long.parseLong(s);
			this.${daoName}.delete(id);
#trim('eachrow', 3, 'rn')
			#delRelation2
#end

		}
	}
	
#set($refFieldLst = $MU.getReferenceFields($model))
#foreach($field in $refFieldLst)
#set($methodName = 'findBy'+$CSU.getHumpWord($field.name))
	@Override
	public List<${model.name}> ${methodName}($field.type $field.name) {
		return this.${daoName}.${methodName}($field.name);
	}
#end

	@Override
	public List<${TYPE}> pageQuery(Page page#allParams($model)) {
		return this.${daoName}.pageQuery(page#allParamVals($model));
	}

#set($mtmList = $MU.getModelMtmList($model))
#foreach($mtm in $mtmList)
	#set($RelationItem = $CSU.getSimpleType($mtm.relation))
	#set($relationItem = $CSU.getVariable($RelationItem))
	#set($Model2 = $CSU.getSimpleType($mtm.anotherModel))
	#set($model2 = $CSU.getVariable($Model2))

	@Override
	public void save${Model2}s(Long ${mtm.relationKey}, String ${mtm.anotherRelationKey}s) {
		${relationItem}DAO.deleteByProperty("${mtm.relationKey}", ${mtm.relationKey});
		
		if(${mtm.relationKey}==null || ${mtm.anotherRelationKey}s==null || ${mtm.anotherRelationKey}s.isEmpty()){
			return;
		}
		String[] idseq = ${mtm.anotherRelationKey}s.split(",");
		Date now = new Date();
		for(String s:idseq){
			Long ${mtm.anotherRelationKey} = Long.parseLong(s);
			${RelationItem} item = new ${RelationItem}();
			item.${CSU.getSetMethod($mtm.relationKey)}(${mtm.relationKey});
			item.${CSU.getSetMethod($mtm.anotherRelationKey)}(${mtm.anotherRelationKey});
			item.setCreated(now);
			item.setModified(now);
			${relationItem}DAO.save(item);
		}
	}

	@Override
	public List<Long> get${Model2}IdList(Long ${mtm.relationKey}) {
		return ${relationItem}DAO.findPropertyList("${mtm.anotherRelationKey}", "${mtm.relationKey}", ${mtm.relationKey});
	}

	@Override
	public List<${Model2}> get${Model2}List(Long ${mtm.relationKey}) {
		List idList =  ${relationItem}DAO.findPropertyList("${mtm.anotherRelationKey}", "${mtm.relationKey}", ${mtm.relationKey});
		return ${model2}DAO.findByIdList(idList);
	}
#end

#foreach($field in $model.fields)
#set($mtm = $MU.getElementMtm($field))
#if($mtm)
	#set($Model2 = $CSU.getSimpleType($mtm.anotherModel))
	#set($model2 = $CSU.getVariable($Model2))
	public ${TYPE} get${TYPE}With${Model2}s(Long id ) {
		${TYPE} ${type} = this.${type}DAO.findById(id);
		${type}.set${Model2}s(this.get${Model2}List(id));
		return ${type};
	}
#end
#end
}
